//	Pixlib
import com.bourre.data.collections.Map;
import com.bourre.events.IEventDispatcher;
import com.bourre.log.Logger;
import com.bourre.log.LogLevel;

//	Oregano
import org.omus.core.Group;

//	Pixlib API for Oregano
import net.webbymx.pixgano.handlers.group.IGroupHandler;


class net.webbymx.pixgano.handlers.group.GroupHandlerManager 
{
/* ****************************************************************************
* PRIVATE STATIC VARIABLES
**************************************************************************** */
	private static var _oI		: GroupHandlerManager;
	
/* ****************************************************************************
* PRIVATE VARIABLES
**************************************************************************** */
	private var _mGH			: Map;				//	Map object holding the pair group / GroupHandler
	private var _ghA			: IGroupHandler;	//	active GroupHandler
	private var _oEB			: IEventDispatcher;


/* ****************************************************************************
* CONSTRUCTOR
**************************************************************************** */
	private function GroupHandlerManager( oEB : IEventDispatcher ) {
		_mGH = new Map();
		_ghA = undefined;
		_oEB = oEB;
	}
	
/**
* singleton access
* @param	oEB	Instance of the EventBroadcaster used
* @return
*/
	public static function getInstance( oEB : IEventDispatcher ) : GroupHandlerManager  
	{
		if ( !_oI ) 
		{
			if ( arguments[ 0 ] == undefined ||  ! ( arguments[ 0 ] instanceof IEventDispatcher ) ) 
			{
				Logger.LOG( "Cannot create the GroupHandlerManager object. You need to pass an EventBroadcaster as a parameter", LogLevel.FATAL );
				throw( "Cannot create the GroupHandlerManager object. You need to pass an EventBroadcaster as a parameter" );
			}
			else {
				_oI = new GroupHandlerManager( oEB );
			}
		}
		return _oI; 
	}
	
	
/* ****************************************************************************
* PUBLIC FUNCTIONS
**************************************************************************** */
/**
* Register to the GroupHandlerManager a GroupHandler for a group
* @param	gID a group name ( String )
* @param	gh a group hangler class ( String )
* @return	Void
*/
	public function registerGroupHandler( gID : String, gh : String ) : Void 
	{
	//	register the new group handler for a group
		if ( _mGH.containsKey( gID ) ) Logger.LOG( "A GroupHandler for the group " + gID + " already exist. It will be replaced by the new one", LogLevel.WARN );
		_mGH.put( gID, gh );
	}
	
	
/**
* Select the GroupHandler for the group name given
* @param	gID
*/
	public function switchGroupHandler( gID:String ) : Void 
	{
/*		Logger.LOG( "GroupHandlerManager :: switchGroupHandler", LogLevel.INFO );		
		Logger.LOG( "Identifiant du groupe : " + gID, LogLevel.INFO );
*/		
		//	get the active GroupHandler and remove it as a listener to the Group singleton
			Group.getInstance().removeListener( _ghA );
		//	delete the handler instance
			delete _ghA;
			
		//	get the package name	
			var sPackage:String = _mGH.get( gID );

		//	get the class
			var clazz : Function = eval( "_global." + sPackage );
			if ( !clazz ) 
			{
				Logger.LOG( "'_global." + sPackage + "' class is not available in the current swf", LogLevel.FATAL );
				throw( "'_global." + sPackage + "' class is not available in the current swf" );
				return;
			}
			
		//	create the instance	
			_ghA = new clazz( _oEB );

		//	add the listener
			Group.getInstance().addListener( _ghA );
			
		//	call the onChange function
			_ghA.onChangeInit();
	}
	
/**
* Return the EventBroadcaster used
* @return
*/
	public function getBroadcaster() : IEventDispatcher { return _oEB }
}
